package com.by.log.service;

import com.by.log.entity.StatisticData;
import com.by.log.handler.StatisticCollector;
import com.by.log.statistic.StatisticManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 统计信息服务类
 * 提供统计信息查询接口供外部系统使用
 */
@Service
public class StatisticService {
    
    /**
     * 获取所有统计信息
     * @return 统计信息列表
     */
    public List<StatisticData> getAllStatistics() {
        Map<String, StatisticCollector> collectors = StatisticManager.getInstance().getAllCollectors();
        List<StatisticData> result = new ArrayList<>();
        
        for (Map.Entry<String, StatisticCollector> entry : collectors.entrySet()) {
            try {
                Map<String, StatisticData> stats = entry.getValue().collect();
                StatisticData data = stats.get(entry.getKey());
                if (data != null) {
                    result.add(data);
                }
            } catch (Exception e) {
                System.err.println("Error collecting stats for " + entry.getKey() + ": " + e.getMessage());
            }
        }
        
        return result;
    }
    
    /**
     * 根据类名获取统计信息
     * @param className 类名
     * @return 统计信息
     */
    public StatisticData getStatisticsByClass(String className) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector != null) {
            try {
                Map<String, StatisticData> stats = collector.collect();
                return stats.get(className);
            } catch (Exception e) {
                System.err.println("Error collecting stats for " + className + ": " + e.getMessage());
            }
        }
        return null;
    }
    
    /**
     * 获取QPS信息
     * @param className 类名
     * @return QPS值
     */
    public double getQPS(String className) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector != null) {
            return collector.getQPS();
        }
        return 0.0;
    }
    
    /**
     * 获取维度的QPS信息
     * @param className 类名
     * @param dimension 维度名称
     * @param targetId 目标标识
     * @return QPS值
     */
    public double getDimensionalQPS(String className, String dimension, String targetId) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector instanceof com.by.log.statistic.DefaultStatisticCollector) {
            com.by.log.statistic.DefaultStatisticCollector defaultCollector = 
                (com.by.log.statistic.DefaultStatisticCollector) collector;
            
            com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics dimensionalStats = 
                defaultCollector.getDimensionalStatistics(dimension, targetId);
            
            if (dimensionalStats != null) {
                return dimensionalStats.getQPS();
            }
        }
        return 0.0;
    }
    
    /**
     * 获取成功率
     * @param className 类名
     * @return 成功率
     */
    public double getSuccessRate(String className) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector != null) {
            return collector.getSuccessRate();
        }
        return 0.0;
    }
    
    /**
     * 获取维度的成功率
     * @param className 类名
     * @param dimension 维度名称
     * @param targetId 目标标识
     * @return 成功率
     */
    public double getDimensionalSuccessRate(String className, String dimension, String targetId) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector instanceof com.by.log.statistic.DefaultStatisticCollector) {
            com.by.log.statistic.DefaultStatisticCollector defaultCollector = 
                (com.by.log.statistic.DefaultStatisticCollector) collector;
            
            com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics dimensionalStats = 
                defaultCollector.getDimensionalStatistics(dimension, targetId);
            
            if (dimensionalStats != null) {
                return dimensionalStats.getSuccessRate();
            }
        }
        return 0.0;
    }
    
    /**
     * 获取平均响应时间
     * @param className 类名
     * @return 平均响应时间
     */
    public double getAverageResponseTime(String className) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector != null) {
            return collector.getAverageResponseTime();
        }
        return 0.0;
    }
    
    /**
     * 获取维度的平均响应时间
     * @param className 类名
     * @param dimension 维度名称
     * @param targetId 目标标识
     * @return 平均响应时间
     */
    public double getDimensionalAverageResponseTime(String className, String dimension, String targetId) {
        StatisticCollector collector = StatisticManager.getInstance().getCollector(className);
        if (collector instanceof com.by.log.statistic.DefaultStatisticCollector) {
            com.by.log.statistic.DefaultStatisticCollector defaultCollector = 
                (com.by.log.statistic.DefaultStatisticCollector) collector;
            
            com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics dimensionalStats = 
                defaultCollector.getDimensionalStatistics(dimension, targetId);
            
            if (dimensionalStats != null) {
                return dimensionalStats.getAverageResponseTime();
            }
        }
        return 0.0;
    }
    
    
    /**
     * 收集所有统计信息（会重置计数器）
     * @return 统计信息映射
     */
    public Map<String, StatisticData> collectAllAndReset() {
        return StatisticManager.getInstance().collectAllAndReset();
    }
    
    /**
     * 根据维度获取统计信息
     * @param dimension 维度名称
     * @param targetId 目标标识
     * @return 统计信息列表
     */
    public List<StatisticData> getStatisticsByDimension(String dimension, String targetId) {
        Map<String, StatisticCollector> collectors = StatisticManager.getInstance().getAllCollectors();
        List<StatisticData> result = new ArrayList<>();
        
        for (Map.Entry<String, StatisticCollector> entry : collectors.entrySet()) {
            try {
                if (entry.getValue() instanceof com.by.log.statistic.DefaultStatisticCollector) {
                    com.by.log.statistic.DefaultStatisticCollector defaultCollector = 
                        (com.by.log.statistic.DefaultStatisticCollector) entry.getValue();
                    
                    Map<String, com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics> dimensionalStats = 
                        defaultCollector.getAllDimensionalStatistics();
                    
                    for (Map.Entry<String, com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics> dimEntry : dimensionalStats.entrySet()) {
                        com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics dimStat = dimEntry.getValue();
                        if (dimension.equals(dimStat.getDimension()) && targetId.equals(dimStat.getTargetId())) {
                            Map<String, StatisticData> stats = dimStat.collect();
                            result.addAll(stats.values());
                        }
                    }
                }
            } catch (Exception e) {
                System.err.println("Error collecting dimensional stats: " + e.getMessage());
            }
        }
        
        return result;
    }
    
    /**
     * 获取特定维度的所有统计信息
     * @param dimension 维度名称
     * @return 统计信息列表
     */
    public List<StatisticData> getAllStatisticsByDimension(String dimension) {
        Map<String, StatisticCollector> collectors = StatisticManager.getInstance().getAllCollectors();
        List<StatisticData> result = new ArrayList<>();
        
        for (Map.Entry<String, StatisticCollector> entry : collectors.entrySet()) {
            try {
                if (entry.getValue() instanceof com.by.log.statistic.DefaultStatisticCollector) {
                    com.by.log.statistic.DefaultStatisticCollector defaultCollector = 
                        (com.by.log.statistic.DefaultStatisticCollector) entry.getValue();
                    
                    Map<String, com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics> dimensionalStats = 
                        defaultCollector.getAllDimensionalStatistics();
                    
                    for (Map.Entry<String, com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics> dimEntry : dimensionalStats.entrySet()) {
                        com.by.log.statistic.DefaultStatisticCollector.DimensionalStatistics dimStat = dimEntry.getValue();
                        if (dimension.equals(dimStat.getDimension())) {
                            Map<String, StatisticData> stats = dimStat.collect();
                            result.addAll(stats.values());
                        }
                    }
                }
            } catch (Exception e) {
                System.err.println("Error collecting dimensional stats: " + e.getMessage());
            }
        }
        
        return result;
    }
}